package com.socket.test1;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;

/**
 * Created by buguniao on 16/4/13.
 */
public class ClientTask implements Runnable{
    private int i;
    public ClientTask(int i){
        this.i=i;
    }
    public void run() {
        try {
            Socket socket = new Socket("localhost",8080);
            OutputStream out = socket.getOutputStream();
            Writer writer = new OutputStreamWriter(out);

            int j=0;
            while(true){
                writer.write(i);
                writer.flush();
                Thread.sleep(1000);
                if(j++==30){
                    break;
                }
            }
            //writer.close();
            socket.close();
            System.out.println("end");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
